using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using ShopForWeb.Models.Basic;

namespace ShopForWeb.Models.DbContexts
{
  public class ShopDbContext : DbContext
  {
    public ShopDbContext(DbContextOptions<ShopDbContext> options) : base(options)
    {

    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
      base.OnModelCreating(modelBuilder);
      modelBuilder.Entity<ShoppingCart>()
                      .HasKey(o => new { o.ProductId, o.UserId });
      modelBuilder.Entity<OrderDetails>()
                      .HasKey(o => new { o.ProductId, o.OrderId });
      modelBuilder.Entity<User>().HasIndex(u => u.Phone).IsUnique();
      modelBuilder.Entity<Admin>().HasData(
      new Admin()
      {
        AdminId = 1,
        Phone = "13955708374",
        Password = "123456",
        Sex = "男",
        TrueName = "Admin"
      }
      );
    }
    public DbSet<User> UserSet { get; set; } = null!;
    public DbSet<Product> ProductSet { get; set; } = null!;
    public DbSet<ShoppingCart> ShoppingCartSet { get; set; } = null!;
    public DbSet<Order> OrderSet { get; set; } = null!;
    public DbSet<Admin> AdminSet { get; set; } = null!;
    public DbSet<OrderDetails> OrderDetails { get; set; } = null!;
  }
}
